//
//  SZCClassificationViewController.m
//  礼物说
//
//  Created by dllo on 16/4/19.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "SZCClassificationViewController.h"

/** 【攻略】专题 CollectionView滑动*/
#define kZHUANTIURL @"http://api.liwushuo.com/v2/collections?limit=6&offset=0"
/** 【攻略】内容 对象，风格，品类*/
#define kGONGLVURL @"http://api.liwushuo.com/v2/channel_groups/all"

/** 【礼物】界面*/
#define kLIWUURL @"http://api.liwushuo.com/v2/item_categories/tree"

#define KURLARRS @[kGONGLVURL, kZHUANTIURL, kLIWUURL]

@interface SZCClassificationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) BaseCollectionView * collecitonView;
@property (nonatomic, retain) UISegmentedControl *segment;

@property (nonatomic, retain) NSMutableArray * bigArr;
@property (nonatomic, retain) UICollectionViewFlowLayout * layout;
@property (nonatomic, retain) NSMutableArray * strategyArr;
@property (nonatomic, retain) NSMutableArray * zhuanTiArr;
@property (nonatomic, retain) NSMutableArray * giftArr;

@end

@implementation SZCClassificationViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc{
    [_giftArr release];
    [_zhuanTiArr release];
    [_strategyArr release];
    [_layout release];
    [_bigArr release];
    [_collecitonView release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [GiFHUD show];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
    [self createSegment];
    [self createCollection];
    [self dataStrategyHandle];
    self.strategyArr = [NSMutableArray array];
}

#pragma mark - 数据解析
/** 【攻略】内容 对象，风格，品类*/
- (void) dataStrategyHandle {
    
    [XRAFNetworkTool GETUrl:kGONGLVURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *data = responseObject[@"data"][@"channel_groups"];
        self.strategyArr = [StrategyBeforeModel modelHandleWithArray:data];

        [self.collecitonView reloadData];
        
        [self dataGifHandle];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}
/** 【礼物】界面*/
- (void)dataGifHandle {
    
    [XRAFNetworkTool GETUrl:kLIWUURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *data = responseObject[@"data"][@"categories"];
        self.giftArr = [GiftBeforeModel modelHandleWithArray:data];
        
        if (_giftArr.count > 0) {
            [self dataZhuanTiHandle];
        }
        
        [GiFHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}
/** 【攻略】专题 CollectionView滑动*/
- (void)dataZhuanTiHandle{
    
    self.zhuanTiArr = [NSMutableArray array];
    
    [XRAFNetworkTool GETUrl:kZHUANTIURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray * data = responseObject[@"data"][@"collections"];
        self.zhuanTiArr = [ZhuanTiModel modelHandleWithArray:data];
        if (_zhuanTiArr.count > 0) {
            
            [_collecitonView reloadData];
        }
        [GiFHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - 创建Segment
- (void)createSegment {
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"攻略", @"礼物"]];
    /** 在navigation上添加一个控件*/
    _segment.frame = CGRectMake(0, 0, kWIDTHS(200), kHEIGHTS(30));
    self.navigationItem.titleView = _segment;
    [_segment release];
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
}
- (void)segmentAction:(UISegmentedControl * )segment{
    
    if (segment.selectedSegmentIndex == 0) {
        
        _collecitonView.contentOffset = CGPointMake(0, 0);
    }else{
        
        _collecitonView.contentOffset = CGPointMake(kWIDTH, 0);
    }
}

#pragma mark - [底部的CollectionView] 创建CollectionView
- (void)createCollection {
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 1;
    _layout.minimumLineSpacing = 1;
    _layout.itemSize = CGSizeMake(kWIDTH, kHEIGHT - 64 - 49);
    _layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collecitonView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 64 - 49) collectionViewLayout:_layout];
    _collecitonView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collecitonView];
    [_collecitonView release];
    [_layout release];
    
    _collecitonView.delegate = self;
    _collecitonView.dataSource = self;
    
    [_collecitonView registerClass:[StrategyCollectionViewCell class] forCellWithReuseIdentifier:@"strategyCell"];
    
    [_collecitonView registerClass:[GiftCollectionViewCell class] forCellWithReuseIdentifier:@"giftCell"];
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        StrategyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"strategyCell" forIndexPath:indexPath];
        cell.zhuanTiArr = _zhuanTiArr;
        cell.strategyArr = _strategyArr;
        
        return cell;
    }else{
        
        GiftCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"giftCell" forIndexPath:indexPath];
        cell.gitfArr = _giftArr;
        return cell;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat effent = scrollView.contentOffset.x;
    if (effent == 0) {
        /** 选择部分指数*/
        _segment.selectedSegmentIndex = 0;
        
    }else{
        
        _segment.selectedSegmentIndex = 1;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
